import * as Cesium from 'cesium';
import { BaseModule } from '@/utils/CesiumManager';

// 图层控制模块
export class LayerControlModule extends BaseModule {
  constructor() {
    super('layerControl');
    this.layers = new Map();
    this.terrainProvider = null;
  }

  init(viewer) {
    super.init(viewer);
  }

  // 添加图层
  addLayer(name, layer, options = {}) {
    if (this.layers.has(name)) {
      console.warn(`Layer ${name} already exists`);
      return;
    }

    // 根据不同类型添加图层
    let cesiumLayer;
    if (layer.type === 'imagery') {
      cesiumLayer = this.viewer.imageryLayers.addImageryProvider(
        new Cesium.UrlTemplateImageryProvider({
          url: layer.url,
          ...options
        })
      );
    } else if (layer.type === 'terrain') {
      // 添加地形图层
      this.viewer.terrainProvider = new Cesium.CesiumTerrainProvider({
        url: layer.url
      });
      cesiumLayer = this.viewer.terrainProvider;
      this.terrainProvider = cesiumLayer;
    } else if (layer.type === 'wms') {
      // 添加WMS图层
      cesiumLayer = this.viewer.imageryLayers.addImageryProvider(
        new Cesium.WebMapServiceImageryProvider({
          url: layer.url,
          layers: layer.layers,
          parameters: layer.parameters || {}
        })
      );
    }

    if (cesiumLayer) {
      this.layers.set(name, {
        layer: cesiumLayer,
        type: layer.type,
        visible: true,
        opacity: layer.opacity || 1.0
      });
    }
  }

  // 显示/隐藏图层
  toggleLayer(name) {
    const layerInfo = this.layers.get(name);
    if (!layerInfo) return;

    if (layerInfo.type === 'imagery') {
      layerInfo.layer.show = !layerInfo.layer.show;
    }
    layerInfo.visible = !layerInfo.visible;
  }

  // 移除图层
  removeLayer(name) {
    const layerInfo = this.layers.get(name);
    if (!layerInfo) return;

    if (layerInfo.type === 'imagery') {
      this.viewer.imageryLayers.remove(layerInfo.layer);
    } else if (layerInfo.type === 'terrain') {
      // 重置为默认地形
      this.viewer.terrainProvider = new Cesium.EllipsoidTerrainProvider();
    }
    this.layers.delete(name);
  }

  // 设置图层透明度
  setLayerOpacity(name, opacity) {
    const layerInfo = this.layers.get(name);
    if (!layerInfo) return;

    if (layerInfo.type === 'imagery') {
      layerInfo.layer.alpha = opacity;
    }
    layerInfo.opacity = opacity;
  }

  // 获取图层透明度
  getLayerOpacity(name) {
    const layerInfo = this.layers.get(name);
    if (!layerInfo) return 1.0;
    return layerInfo.opacity;
  }

  // 获取所有图层状态
  getLayersStatus() {
    const status = {};
    for (const [name, info] of this.layers) {
      status[name] = info.visible;
    }
    return status;
  }

  // 添加地形图层
  addTerrainLayer(name, url) {
    this.addLayer(name, {
      type: 'terrain',
      url: url
    });
  }

  // 添加区划图层(WMS)
  addWmsLayer(name, url, layers, parameters = {}) {
    this.addLayer(name, {
      type: 'wms',
      url: url,
      layers: layers,
      parameters: parameters
    });
  }

  // 添加WMTS图层
  addWmtsLayer(name, url, layer, style, format, tileMatrixSetID, maximumLevel) {
    const wmtsProvider = new Cesium.WebMapTileServiceImageryProvider({
      url: url,
      layer: layer,
      style: style,
      format: format,
      tileMatrixSetID: tileMatrixSetID,
      maximumLevel: maximumLevel,
      credit: name
    });
    
    const cesiumLayer = this.viewer.imageryLayers.addImageryProvider(wmtsProvider);
    
    this.layers.set(name, {
      layer: cesiumLayer,
      type: 'wmts',
      visible: true,
      opacity: 1.0
    });
  }

  // 添加影像图层
  addImageryLayer(name, url, options = {}) {
    this.addLayer(name, {
      type: 'imagery',
      url: url,
      ...options
    });
  }

  // 添加矢量图层 (GeoJSON)
  addVectorLayer(name, url, options = {}) {
    const dataSource = new Cesium.GeoJsonDataSource.load(url, {
      stroke: Cesium.Color.BLUE,
      fill: Cesium.Color.BLUE.withAlpha(0.3),
      strokeWidth: 3,
      markerSymbol: '?',
      ...options
    });
    
    dataSource.then((dataSource) => {
      this.viewer.dataSources.add(dataSource);
      this.layers.set(name, {
        layer: dataSource,
        type: 'vector',
        visible: true,
        opacity: 1.0
      });
    });
  }

  // 添加图片服务图层
  addImageLayer(name, url, rectangle, options = {}) {
    const imageProvider = new Cesium.SingleTileImageryProvider({
      url: url,
      rectangle: rectangle ? Cesium.Rectangle.fromDegrees(...rectangle) : undefined,
      ...options
    });
    
    const cesiumLayer = this.viewer.imageryLayers.addImageryProvider(imageProvider);
    
    this.layers.set(name, {
      layer: cesiumLayer,
      type: 'image',
      visible: true,
      opacity: 1.0
    });
  }

  // 添加ArcGIS地图服务图层
  addArcGISMapServerLayer(name, url, options = {}) {
    const esriProvider = new Cesium.ArcGisMapServerImageryProvider({
      url: url,
      ...options
    });
    
    const cesiumLayer = this.viewer.imageryLayers.addImageryProvider(esriProvider);
    
    this.layers.set(name, {
      layer: cesiumLayer,
      type: 'arcgis',
      visible: true,
      opacity: 1.0
    });
  }

  // 图层分栏显示
  showLayerPanel() {
    // 这个方法应该在UI组件中实现
    console.log('显示图层分栏面板');
  }

  // 关闭图层分栏
  hideLayerPanel() {
    // 这个方法应该在UI组件中实现
    console.log('关闭图层分栏面板');
  }
}